package com.zh.controller;


import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zh.Exception.CategoryExistsDishException;
import com.zh.Exception.DuplicateException;
import com.zh.common.Result;
import com.zh.entity.Category;
import com.zh.entity.Dish;
import com.zh.service.CategoryService;
import com.zh.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 菜品及套餐分类 前端控制器
 * </p>
 *
 * @author 你爹
 * @since 2023-09-21
 */
@RestController
@RequestMapping("/category")
@Slf4j
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private DishService dishService;

    @GetMapping("/page")
    public Result<Page> page(@RequestParam("page") Integer page, @RequestParam("pageSize") Integer pageSize) {
        log.info("分类管理 page:{} pageSize{}", page, pageSize);
        Page<Category> p = new Page<>(page, pageSize);
        LambdaUpdateWrapper<Category> wrapper = new LambdaUpdateWrapper<>();
        wrapper.orderByAsc(Category::getSort);
        categoryService.page(p, wrapper);
        return Result.success(p);
    }

    @PostMapping
    public Result save(@RequestBody Category category) throws DuplicateException {
        log.info("保存分类:{}", category);
        LambdaUpdateWrapper<Category> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Category::getName, category.getName());
        Category category1 = categoryService.getOne(wrapper);
        if (category1 != null) {
            throw new DuplicateException(category.getName() + "分类");
        }
        categoryService.save(category);
        return Result.success();
    }

    @DeleteMapping
    public Result delete(@RequestParam("id") Long id) throws CategoryExistsDishException {
        log.info("删除分类:{}", id);
        LambdaUpdateWrapper<Dish> wrapper = new LambdaUpdateWrapper();
        wrapper.eq(Dish::getCategoryId, id);
        List<Dish> dishes = dishService.list(wrapper);
        Category category = categoryService.getById(id);
        if (dishes != null && dishes.size() > 0) {
            throw new CategoryExistsDishException(category.getName());
        }
        //TODO 改逻辑删除
        categoryService.removeById(id);
        return Result.success();
    }

    @PutMapping
    public Result update(@RequestBody Category category) throws DuplicateException {
        log.info("修改分类:{}", category);
        categoryService.updateById(category);
        return Result.success();
    }

    @GetMapping("/list")
    public Result<List<Category>> list(Integer type) {
        if (type != null) {
            log.info("查询分类类型:{}", type == 1 ? "菜品" : "套餐");
        }
        LambdaUpdateWrapper<Category> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(type != null, Category::getType, type);
        wrapper.orderByAsc(Category::getSort);
        List<Category> list = categoryService.list(wrapper);
        return Result.success(list);
    }
}

